<%--
  Created by IntelliJ IDEA.
  User: Zafkiel
  Date: 2021/8/14
  Time: 18:25
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="service.AccountService" %>
<%@ page import="static servlet.UserInfoServlet.doLogin" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>靓仔购物网 - 登录</title>

    <!--用百度的静态资源库的cdn安装bootstrap环境-->
    <!-- Bootstrap 核心 CSS 文件 -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">
    <!--font-awesome 核心我CSS 文件-->
    <link href="font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 在bootstrap.min.js 之前引入 -->
    <script src="./js/jquery-3.2.1.min.js"></script>
    <!-- Bootstrap 核心 JavaScript 文件 -->
    <script src="./js/bootstrap.min.js"></script>
    <script src="./js/sha256.min.js"></script>
    <script src="./js/account.js"></script>
    <link rel="stylesheet" href="css/login.css">

</head>
<body onload="autoLog()">

<!-- 头部 logo -->
<header class="container">
    <div class="row" style="padding-top: 20px;">
        <div class="col-md-2"><a href="#"><img src="img/logo.png" /></a></div>
        <div class="col-md-2" style="font-size: 30px; line-height:70px;">欢迎登录</div>
    </div>
</header>

<!-- body 背景图片 和 表单 -->
<div class="container body">
    <div class="form row formRow">
        <form action="<%=application.getContextPath()%>/login.userInfoService" method="post" class="loginFormBox" id="loginform">
            <h3 class="form-title">登录</h3>
            <div class="loginFormBox">
                <div class="form-group">
                    <!--        用户名图标和用户名输入框            -->
                    <i class="fa fa-user" aria-hidden="true"></i>
                    <input class="form-control" type="text" name="username" id="username" placeholder="请输入用户名" required autofocus>
                </div>
                <!--        密码图标和密码输入框                -->
                <div class="form-group">
                    <i class="fa fa-key" aria-hidden="true"></i>
                    <input class="form-control" type="password" name="psd" id="psd" placeholder="请输入密码" required>
                    <input type="hidden" name="password" id="password">
                </div>
                <!--        用超链接跳转到注册页面               -->
                <div class="form-group">
                    <a href="register.jsp" class="pull-left">还没注册？</a>
                    <a href="forgetpwd.jsp" target="_blank" class="pull-right">忘记密码？</a>
                </div>
                <!--登录按钮-->
                <div class="form-group ">
                    <input type="button" value="登录" class="btn btn-success" id="btn-login" style="width: 100%;">
                </div>
                <div class="form-group">
                    <a href="register.jsp"><img src="img/qq.png" />QQ</a>&nbsp;|
                    <a href="register.jsp"><img src="img/weixin.png"/>微信</a>
                    <input type="checkbox" id="autolog" name="autolog" value="true" style="float: right;">
                    <span style="float: right;margin-right: 5px;">自动登录</span>
                </div>
            </div>
        </form>
    </div>
</div>

<footer class="container-fluid">
    版权所有: 南京市艾瑞职业培训学校 ICP备案: 苏ICP备15022503号-1
</footer>
<script type="text/javascript">
    $(function () {
        <%
            AccountService.LoginInfo loginState = (AccountService.LoginInfo)session.getAttribute("OESloginInfo");
//System.out.println("login.jsp: " + (loginState == null? "null": loginState.getLoginState()));
            if(loginState != null) switch (loginState.getLoginState()) {
                //如果已经登录或上一次登录失败
                case FAIL:
                    if(loginState.getUserInfo() != null) {%>
                        document.getElementById("username").value = "<%=loginState.getUserInfo().getU_name()%>";
                    <%}%>
                    alert("登录失败，请检查登录名和口令是否正确");
                    document.getElementById("psd").focus();
                    <%
                    break;
                case ACCOUNTDISABLED: %>alert("登录失败，账号已停用");<% break;
                case ACCOUNTBANNED:   %>alert("登录失败，账号被封禁");<% break;
                case SUCCEED:         {
                    String uname = loginState.getUserInfo().getU_name();
                    String upass = loginState.getUserInfo().getU_passwd();

                    doLogin(uname, upass, "false", request.getContextPath() + "/main.jsp", request.getContextPath() + "/login.jsp", session, response);
                    break;
                }
                case EXCEPTION:  default: %>alert("登录异常，异常码：<%=loginState%>");<% break;
            } else {
                //如果本次会话未登录，检查自动登录
                String uname = null;
                String upass = null;
                Boolean auto = null;

                Cookie[] cookies = request.getCookies();
                if(cookies != null) for(Cookie each : cookies) {
                    if("OESloginAuto".equals(each.getName())) {
                        if("true".equals(each.getValue())) auto = true; else break;
                    }

                    if("OESloginName".equals(each.getName())) uname = each.getValue();
                    if("OESloginPass".equals(each.getName())) upass = each.getValue();
                    if(uname != null  &&  upass != null  &&  auto != null  &&  auto) {
                        doLogin(uname, upass, "false", request.getContextPath() + "/main.jsp", request.getContextPath() + "/login.jsp", session, response);
                        break;
                    }
                }
            }
        %>

        $("#btn-login").click(function () {

            var btnLogin = document.getElementById("btn-login");
            btnLogin.disabled = true;

            var psdInput = document.getElementById("psd");
            var uname = $("#username").val();
            var upass = psdInput.value;

            if(uname == null  ||  upass == null  ||  uname == ""  ||  upass == "") alert("请填写完整的信息"); else {
                if(!accountStrTest(uname)) {
                    alert("用户名长度应为8到16个字符");
                    document.getElementById("psd").value = "";
                    btnLogin.disabled = false;
                    return;
                }
                if(!accountStrTest(upass)) {
                    alert("密码长度应为8到16个字符");
                    document.getElementById("psd").value = "";
                    btnLogin.disabled = false;
                    return;
                }

                var psd = document.getElementById("password");
                //对用户口令进行第一道SHA256加密
                psd.value = sha256.hex(upass);

                document.getElementById("loginform").submit();

                psd.value = "";
            }
            btnLogin.disabled = false;
        });
    });
</script>
</body>
</html>
